﻿using ProjectScheduling.daos;

namespace ProjectScheduling
{
    public partial class ProjectTypePage : UserControl
    {
        public static readonly ProjectTypePage b = new();
        private readonly AddProjectTypeForm addform = AddProjectTypeForm.b;
        public ProjectTypePage()
        {
            InitializeComponent();
            view_data.AutoGenerateColumns = false;
            addform.action = Search;
            Search();
        }
        private void Search()
        {
            var table = ProjectTypeDao.Base.ToTable("SELECT * FROM ProjectTypes WHERE @key='' OR Name LIKE CONCAT('%',@key,'%')", ("@key", txt_search_key.Text ?? ""));
            view_data.DataSource = table;
        }
        private void Btn_search_Click(object sender, EventArgs e) => Search();

        private void Btn_add_Click(object sender, EventArgs e)
        {
            addform.FindById(0);
            addform.ShowDialog();
        }

        private void View_data_SelectionChanged(object sender, EventArgs e) => view_data.ClearSelection();

        private void View_data_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            var id = int.Parse(view_data.Rows[e.RowIndex].Cells[0].Value.ToString() ?? "0");
            if (e.ColumnIndex == 3)
            {
                addform.FindById(id);
                addform.ShowDialog();
                return;
            }
            if (e.ColumnIndex == 4)
            {
                if (MessageBox.Show("确定要删除当前类型吗？", "删除确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    ProjectTypeDao.Base.Delete(id);
                    Search();
                }
            }
        }
    }
}
